﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WordList
{
    class WordService
    {
        public static int ReciteNum = 5;//每次要新背诵的单词数
        public static List<Word> RandomSort(List<Word> words)//打乱单词列表的顺序
        {
            var random = new Random();
            var newList = new List<Word>();
            foreach (var item in words)
            {
                newList.Insert(random.Next(newList.Count), item);
            }
            return newList;
        }
        public static List<Word> GetRecite(List<Word> words)//获取本次要背诵的单词
        {
            List<Word> words0 = words.Where(wd => wd.Times == 0).ToList();//选出没背过的单词
            List<Word> words1 = RandomSort(words0);//打乱顺序
            List<Word> words2 = new List<Word>();
            Console.WriteLine("Revise List:");
            for (int i = 0; i < ReciteNum && i < words1.Count; i++)//取前ReviseNum个单词
            {
                Console.WriteLine(words1[i].Text + " " + words1[i].Proficiency + " " + words1[i].Times); ;
                words2.Add(words1[i]);
            }
            return words2;
        }
        public static List<Word> GetReview(List<Word> words)
        {
            List<Word> words0 = words.Where(wd => wd.Times != 0).ToList();
            List<Word> words1 = words0.OrderBy(wd => wd.Times).ToList();
            List<Word> words01 = words1.Where(wd => wd.Proficiency == 0).ToList();
            List<Word> words02 = words1.Where(wd => wd.Proficiency == 1).ToList();
            List<Word> words03 = words1.Where(wd => wd.Proficiency == 2).ToList();
            List<Word> words11 = words01.Take(Convert.ToInt32(0.7 * words01.Count)).ToList();
            List<Word> words12 = words02.Take(Convert.ToInt32(0.2 * words02.Count)).ToList();
            List<Word> words13 = words03.Take(Convert.ToInt32(0.1 * words03.Count)).ToList();
            List<Word> list1 = words11.Union(words12).ToList();
            List<Word> list2 = list1.Union(words13).ToList();
            Console.WriteLine("Review List:");
            foreach (Word wd in list2)
            {
                Console.WriteLine(wd.Text + " " + wd.Proficiency + " " + wd.Times); ;
            }
            return list2;
        }
        public static void ReciteWord(Word word)
        {
            Console.WriteLine(word.Text);
            int impression;
            bool result = false;
            do
            {
                Console.WriteLine("请选择对该词的印象:(1:熟悉 2:会 3:不会)");
                result = Int32.TryParse(Console.ReadLine(), out impression);
            } while (result == false || (impression != 1 && impression != 2 && impression != 3));
            switch (impression)
            {
                case 1:
                    word.Proficiency = 2;
                    break;
                case 2:
                    word.Proficiency++;
                    break;
                case 3:
                    break;
            }
            if (word.Proficiency > 2) word.Proficiency = 2;
            word.Times++;
        }
        public static void Recite(List<Word> words0)
        {
            List<Word> words = GetRecite(words0);
            foreach(Word wd in words)
            {
                ReciteWord(wd);
            }
        }
        public static void Review(List<Word> words0)
        {
            List<Word> words = GetReview(words0);
            foreach(Word wd in words)
            {
                ReciteWord(wd);
            }
        }
    }
}
